package com.example.demo.demos.util;

import org.apache.commons.lang3.StringUtils;

public class Log4j2Config {

    public Log4j2Config() {
    }

    public static final void init(LogConfigBean logConfigBean) {
        String level = logConfigBean.getLevel();
        String path = logConfigBean.getPath();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<?xml version='1.0' encoding='UTF-8'?>\n");
        stringBuilder.append("\n");
        stringBuilder.append("<configuration status='DEBUG' monitorInterval='30'>\n");
        stringBuilder.append("\n");
        stringBuilder.append("\t<properties>\n");
        stringBuilder.append("        <property name='FORMAT'>%highlight{[%p] %t %-d{yyyy-MM-dd HH:mm:ss:SSS} %n  %l%n  %m%n}{FATAL=red, ERROR=red, WARN=yellow, INFO=blue, DEBUG=green,TRACE=cyan}</property>\n");
        stringBuilder.append("    </properties>\n");
        stringBuilder.append("\n");
        stringBuilder.append("    <appenders>\n");
        stringBuilder.append("\n");
        stringBuilder.append("        <Console name='Console' target='SYSTEM_OUT'>\n");
        stringBuilder.append("            <PatternLayout pattern='${FORMAT}'/>\n");
        stringBuilder.append("        </Console>\n");
        stringBuilder.append("        \n");
        stringBuilder.append("        <RollingFile name='FILE_DEBUG' filePattern='${log.file.path}/${date:yyyy-MM-dd}/debug-%d{yyyy-MM-dd}-%i.log'>\n");
        stringBuilder.append("            <!--控制台只输出level及以上级别的信息（onMatch），其他的直接拒绝（onMismatch）-->        \n");
        stringBuilder.append("            <ThresholdFilter level='DEBUG' onMatch='ACCEPT' />\n");
        stringBuilder.append("            <PatternLayout pattern='${FORMAT}' />\n");
        stringBuilder.append("            <Policies>\n");
        stringBuilder.append("                <TimeBasedTriggeringPolicy />\n");
        stringBuilder.append("                <SizeBasedTriggeringPolicy size='100 MB' />\n");
        stringBuilder.append("            </Policies>\n");
        stringBuilder.append("        </RollingFile>\n");
        stringBuilder.append(" \n");
        stringBuilder.append("        <RollingFile name='FILE_INFO' filePattern='${log.file.path}/${date:yyyy-MM-dd}/info-%d{yyyy-MM-dd}-%i.log'>\n");
        stringBuilder.append("            <!--控制台只输出level及以上级别的信息（onMatch），其他的直接拒绝（onMismatch）-->        \n");
        stringBuilder.append("            <ThresholdFilter level='INFO' onMatch='ACCEPT' />\n");
        stringBuilder.append("            <PatternLayout pattern='${FORMAT}'/>\n");
        stringBuilder.append("            <Policies>\n");
        stringBuilder.append("                <TimeBasedTriggeringPolicy/>\n");
        stringBuilder.append("                <SizeBasedTriggeringPolicy size='100 MB'/>\n");
        stringBuilder.append("            </Policies>\n");
        stringBuilder.append("        </RollingFile>\n");
        stringBuilder.append(" \n");
        stringBuilder.append("        <RollingFile name='FILE_WARN' filePattern='${log.file.path}/${date:yyyy-MM-dd}/warn-%d{yyyy-MM-dd}-%i.log'>\n");
        stringBuilder.append("           \t<ThresholdFilter level='WARN' onMatch='ACCEPT' />\n");
        stringBuilder.append("            <PatternLayout pattern='${FORMAT}'/>\n");
        stringBuilder.append("            <Policies>\n");
        stringBuilder.append("                <TimeBasedTriggeringPolicy/>\n");
        stringBuilder.append("                <SizeBasedTriggeringPolicy size='100 MB'/>\n");
        stringBuilder.append("            </Policies>\n");
        stringBuilder.append("        </RollingFile>\n");
        stringBuilder.append(" \n");
        stringBuilder.append("        <RollingFile name='FILE_ERROR' filePattern='${log.file.path}/${date:yyyy-MM-dd}/error-%d{yyyy-MM-dd}-%i.log'>\n");
        stringBuilder.append("            <PatternLayout pattern='${FORMAT}'/>\n");
        stringBuilder.append("            <ThresholdFilter level='ERROR' onMatch='ACCEPT' />\n");
        stringBuilder.append("            <Policies>\n");
        stringBuilder.append("                <TimeBasedTriggeringPolicy/>\n");
        stringBuilder.append("                <SizeBasedTriggeringPolicy size='128 MB'/>\n");
        stringBuilder.append("            </Policies>\n");
        stringBuilder.append("        </RollingFile>\n");
        stringBuilder.append(" \n");
        stringBuilder.append("    </appenders>\n");
        stringBuilder.append(" \n");
        stringBuilder.append("    <loggers>\n");
        stringBuilder.append("\n");
        stringBuilder.append("        <root level='${log.level}'>\n");
        stringBuilder.append("            <appender-ref ref='Console'/>\n");
        stringBuilder.append("            <appender-ref ref='FILE_DEBUG'/>\n");
        stringBuilder.append("            <appender-ref ref='FILE_INFO'/>\n");
        stringBuilder.append("            <appender-ref ref='FILE_WARN'/>\n");
        stringBuilder.append("            <appender-ref ref='FILE_ERROR'/>\n");
        stringBuilder.append("        </root>\n");
        stringBuilder.append("\n");
        stringBuilder.append("        <logger name='org.springframework' level='ERROR' />\n");
        stringBuilder.append("        <logger name='org.apache' level='ERROR' />\n");
        stringBuilder.append("        <logger name='org.mybatis' level='ERROR' />\n");
        stringBuilder.append("        <logger name='org.mongodb' level='ERROR' />\n");
        stringBuilder.append("        <logger name='redis.clients' level='ERROR' />\n");
        stringBuilder.append("        <logger name='org.activiti' level='ERROR' />\n");
        stringBuilder.append("        <logger name='com.alibaba' level='ERROR' />\n");
        stringBuilder.append("\n");
        stringBuilder.append("    </loggers>\n");
        stringBuilder.append(" \n");
        stringBuilder.append("</configuration>");
        String template = stringBuilder.toString();
        String url = path + "/log4j2.xml";
        String content = StringUtils.replaceAll(template, "${log.level}", level);
        content = StringUtils.replaceAll(content, "${log.file.path}", path);
//        FileUtil.fileDelete(url);
//        FileUtil.fileWriteNew(url, content, true);
        configLoad(url);
    }

    public static void configLoad(String url) {
    }
}
